// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //登录信息
    ava:'',
    nickname:'',
    isuser:'',
    logintarget1:'true',
    logintarget2:'none',
    //订单状态
    orderstate:[
      {
        index:0,
        value:0,
        text:'待付款',
        url:'/pic/待付款_sel.png'
      },{
        index:1,
        value:0,
        text:'待发货',
        url:'/pic/待发货_sel.png'
      },{
        index:2,
        value:0,
        text:'租赁中',
        url:'/pic/交易.png'
      },{
        index:3,
        value:0,
        text:'待归还',
        url:'/pic/待收货_sel.png'
      },{
        index:4,
        value:0,
        text:'待评价',
        url:'/pic/评价_sel.png'
      },{
        index:5,
        value:0,
        text:'售后服务',
        url:'/pic/售后服务.png'
      },
    ],
    //工具栏
    tool:[
      {
        index:0,
        text:'我的发布',
        url:'/pic/发布管理.png'
      },{
        index:1,
        text:'收货地址',
        url:'/pic/收货地址.png'
      },{
        index:2,
        text:'余额充值',
        url:'/pic/交易.png'
      },{
        index:3,
        text:'联系客服',
        url:'/pic/联系客服.png'
      }
    ],
    //充值标识
    charge: 'none',
    //充值金额
    summoney:0,
    money:'',
    collectnum:0

  },
  onLoad(){
    //更新用户信息
    var that = this;
    var openid  = wx.getStorageSync('openid');
    var userinfo =  wx.getStorageSync('userinfo');
    //查找用户信息
    
    wx.request({
      url: 'http://127.0.0.1:8080/user/updateWxInfo',
      method:"POST",
      data:{
        openid:openid,
        nickname:userinfo.nickName,
        ava:userinfo.avatarUrl
      },
      success(res){
        // console.log(res)
      }
    })

  },

  /**
   onshow每次点击页面都会加载 
   */
  onShow() {
   
    let that = this
    that.onLoad();
    if(wx.getStorageSync('openid')){
      var openid  = wx.getStorageSync('openid');
          var userinfo =  wx.getStorageSync('userinfo');
          that.setData({
            ava:userinfo.avatarUrl,
            nickname:userinfo.nickName,
            logintarget1:'none',
            logintarget2:'true'
          })
        
         //渲染用户身份状态 
        wx.request({
          url: 'http://127.0.0.1:8080/user/login',
          method:"POST",
          data:{openid:openid},
          success(res){
            // console.log(res)
            var result = res.data;
            wx.setStorageSync('identity', res.data.iden)
            wx.setStorageSync('openid',openid);
            //身份是否验证
          if(result.infostate == "已通过"){
            wx.setStorageSync('isuser', "yes")
            that.setData({
              isuser:true,
            })
            if(wx.getStorageSync('identity') == "学生用户"){
              that.setData({
                summoney:parseFloat(result.money)
              })
            }
          }else if(result.infostate == "待审核"){
            wx.setStorageSync('isuser', "wait")
            that.setData({
              isuser:false
            })
          } else{
            wx.setStorageSync('isuser', "no")
            that.setData({
              isuser:false
            })
          }
          }
        })

        if(wx.getStorageSync('identity') == "学生用户"){
                  //渲染收藏信息
    wx.request({
      url: 'http://127.0.0.1:8080/itemCollect/showItemCollect',
      method:"POST",
      data:{userid:wx.getStorageSync('openid')},
      success(res){
        if(res.data.length != 0){
          that.setData({
            collectnum:res.data.length
          })
        }
        
      }
    })

        //获取当前用户的全部订单
        //待付款
        wx.request({
          url: 'http://127.0.0.1:8080/order/selectOrderNoPay',
          method:"POST",
          data:{
            openid:openid
          },
          success(res){
            // console.log(res);
            var len = res.data.length;
            var orderstate = 'orderstate[0].value';
            that.setData({
              [orderstate]:len
            })
          }
        })

        //待发货
        wx.request({
          url: 'http://127.0.0.1:8080/order/userOrderWaitItem',
          method:"POST",
          data:{
            openid:openid
          },
          success(res){
            // console.log(res);
            var len = res.data.length;
            var orderstate = 'orderstate[1].value';
            that.setData({
              [orderstate]:len
            })
          }
        })

        //租赁中
        wx.request({
          url: 'http://127.0.0.1:8080/order/userOrderInRent',
          method:"POST",
          data:{
            openid:openid
          },
          success(res){
            // console.log(res);
            var len = res.data.length;
            var orderstate = 'orderstate[2].value';
            that.setData({
              [orderstate]:len
            })
          }
        })

        //待归还
        wx.request({
          url: 'http://127.0.0.1:8080/order/userOrderBack',
          method:"POST",
          data:{
            openid:openid
          },
          success(res){
            // console.log(res);
            var len = res.data.length;
            var orderstate = 'orderstate[3].value';
            that.setData({
              [orderstate]:len
            })
          }
        })

        //待评价
        wx.request({
          url: 'http://127.0.0.1:8080/order/userOrderFinish',
          method:"POST",
          data:{
            openid:openid
          },
          success(res){
            // console.log(res);
            var len = res.data.length;
            var orderstate = 'orderstate[4].value';
            that.setData({
              [orderstate]:len
            })
          }
        })

        //售后服务
        wx.request({
          url: 'http://127.0.0.1:8080/order/mineRent',
          method:"POST",
          data:{openid:openid},
            success(res){
              console.log(res)
              var len = res.data.length;
              var orderstate = 'orderstate[5].value';
              that.setData({
                [orderstate]:len
              })
            }
        })

        }
    }
  },

  //登录查询是否有该用户
  login(e){
    let that  = this;
    wx.getUserProfile({
      lang:'zh_CN',
      desc: '用户登录',
      success(res){
        // console.log(res)
        var userinfo =  res.userInfo;
        wx.setStorageSync('userinfo', userinfo)
        that.setData({
          ava:userinfo.avatarUrl,
          nickname:userinfo.nickName,
          logintarget1:'none',
          logintarget2:'true'
        })
       wx.login({
         success: (res) => {
          // console.log(res)
          var code  = res.code;
          // 用code获取用户的openid以及sessionkey
          wx.request({
            url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx6364fd65abca6cb1&secret=a7ca79e76d2af4a2c9b78a35ff707e2b&js_code=${code}&grant_type=authorization_code`, 
            success:(res)=>{
              // console.log(res)
              var openid = res.data.openid;
              // 用openid去
              wx.request({
                url: 'http://127.0.0.1:8080/user/login',
                method:"POST",
                data:{openid:openid},
                success(res){
                  // console.log(res)
                  var result = res.data
                  wx.setStorageSync('openid',openid);
                  wx.setStorageSync('identity',res.data.iden);
                  that.onShow();
                  //身份是否验证
                if(result.infostate == "已通过"){
                  wx.setStorageSync('isuser', "yes")
                  that.setData({
                    isuser:true,
                    summoney:result.money
                  })
                }else if(result.infostate == "待审核"){
                  wx.setStorageSync('isuser', "wait")
                  that.setData({
                    isuser:false
                  })
                } else{
                  wx.setStorageSync('isuser', "no")
                  that.setData({
                    isuser:false
                  })
                }
                }
              })
            }
          })
         },
       })        
      }
    })
  },

  //认证身份
  identity(e){
    if(wx.getStorageSync('openid') != ""){
      wx.navigateTo({
        url: '/pages/identity/identity',
      })
    }else{
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
    }
   
  },
  //收藏
  collect(e){
    if(wx.getStorageSync('openid') == ""){
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
    }else if(wx.getStorageSync('isuser') != "yes"){
      wx.showToast({
        title: '用户身份未验证',
        icon:"none"
      })
    }else{
      wx.navigateTo({
        url: '/pages/collect/collect',
      })
    }
    
  },
  //全部订单
  allorder(e){
    if(wx.getStorageSync('openid') == ""){
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
    }else if(wx.getStorageSync('isuser') != "yes"){
      wx.showToast({
        title: '用户身份未验证',
        icon:"none"
      })
    }else if(wx.getStorageSync('identity') == "房屋出租方"){
      wx.showToast({
        title: '房屋出租方不能使用',
        icon:"none"
      })
    }else{
    wx.navigateTo({
      url: '/pages/order/order?index='+-1+'',
    })
  }
  },
  //订单不同状态
  orderstate(e){
    if(wx.getStorageSync('openid') == ""){
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
    }else if(wx.getStorageSync('isuser') != "yes"){
      wx.showToast({
        title: '用户身份未验证',
        icon:"none"
      })
    }else if(wx.getStorageSync('identity') == "房屋出租方"){
      wx.showToast({
        title: '房屋出租方不能使用',
        icon:"none"
      })
    }else{
    var index = e.currentTarget.dataset.index
    // console.log(index)
    // if(index == 0){
    //   wx.navigateTo({
    //     url: '/pages/shop/shop',
    //   })
    // }else{
      wx.navigateTo({
        url: '/pages/order/order?index='+index+'',
      })
    // }
    }
   
    
  },
  //工具栏
  tool(e){
    if(wx.getStorageSync('openid') == ""){
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
    }else if(wx.getStorageSync('isuser') != "yes"){
      wx.showToast({
        title: '用户身份未验证',
        icon:"none"
      })
    }else{
    // console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index;
    var isuser = wx.getStorageSync('isuser');
    if(index == 0){
      wx.navigateTo({
        url: '/pages/mysubmit/mysubmit',
      })
    }else if(index == 1){
      if(wx.getStorageSync('identity') == "房屋出租方"){
        wx.showToast({
          title: '房屋出租方不能使用',
          icon:"none"
        })
      }else{
         wx.navigateTo({
        url: '/pages/address/address',
      })
      }
     
    }else if(index == 2){
      if(wx.getStorageSync('identity') == "房屋出租方"){
        wx.showToast({
          title: '房屋出租方不能使用',
          icon:"none"
        })
      }else{
              if(isuser == "yes"){
        this.setData({
          charge:true  
        })
      }else{
        wx.showToast({
          title: '非认证用户无法充值',
          duration: 2000,
          icon: "none",
          mask: true,
          success: (res) => {},
        })
      }
      }
      
    }else{
      wx.makePhoneCall({
        phoneNumber: '16324510287',
      })
    }
  }
  },
  //获取充值金额
  getmoney(e){
    var value = e.detail.value;
    this.setData({
      money:value
    })
  },
  //确认充值
  sure(e){
    if(this.data.money == ''){
      wx.showToast({
        title: '输入内容为空',
        icon:"error",
        duration:1000
      })
    }else{
      var that = this;
      var openid = wx.getStorageSync('openid');
      var finalmoney = parseFloat(this.data.money) + parseFloat(this.data.summoney);
      wx.request({
        url: 'http://127.0.0.1:8080/user/updateMoney',
        method:"POST",
        data:{
          openid:openid,
          money:finalmoney
        },
        success(res){
          // console.log(res);
          if(res.data){
            that.setData({
              summoney:finalmoney
            })
          }
        }
      })
      this.setData({
        charge:'none',
        money:''
      })
      // wx.startPullDownRefresh()
    }
  },
  //取消充值
  cancel(e){
      this.setData({
        charge:'none',
        money:''
      })
  }

})